
/*
 * Desenvolvimento para WEB
 * 
 * NOME: Vitor Augusto Graciani           RA: 345270
 * NOME: Felipe Petruz Brina              RA: 345083
 * NOME: Diego Zamboni Latance            RA: 344990
 * NOME: Rafael Claudio Domene            RA: 345040
 * NOME: Vinicius Rodrigues de Campos     RA: 345350
 * 
 */


//var usuarioCadastrado = null;
//var senhaCadastrado = null;
//var nomeCadastrado = null;

var erEmail = /^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/;
var erNome = /^[A-Za-z]{3,16}$/;
var erSenha = /^[a-zA-Z0-9]{6,18}$/;

function verificaCadastroEntidade(){    

    var displayname = document.getElementById("dpname").value;
    var dominio = document.getElementById("nomeDominio").value;
    var tipo = document.getElementById("tipo").value;
    
    if(displayname == ""){
        document.getElementById("labelInsereDisplayname").innerHTML = " * Nome Invalido";
        document.getElementById("labelInsereDisplayname").style.color = "red";
        document.getElementById("labelInsereDisplayname").style.visibility = "visible";
        return false;
    }

    if(dominio == "" || (dominio.match(erName) == null)){
        document.getElementById("labelInsereDominio").innerHTML = " * Categoria Invalida";
        document.getElementById("labelInsereDominio").style.color = "red";
        document.getElementById("labelInsereDominio").style.visibility = "visible";
        return false;
    }

    if(tipo == ""){
        document.getElementById("labelInsereTipo2").innerHTML = " * Tipo Invalido";
        document.getElementById("labelInsereTipo2").style.color = "red";
        document.getElementById("labelInsereTipo2").style.visibility = "visible";
        return false;
    }

    document.getElementById("botaoCadastrarEntidade").type = "submit";
    return true;
 
}
            
function verificaCadastroCategoria(){

    var dominio = document.getElementById("categoria").value;
                
    if(dominio == "" || (dominio.match(erName) == null)){
        document.getElementById("labelInsereDominio").innerHTML = " * Categoria Invalida";
        document.getElementById("labelInsereDominio").style.color = "red";
        document.getElementById("labelInsereDominio").style.visibility = "visible";
        
        return false;
    }
    
    document.getElementById("cadastrarColaborador").type = "submit";
}
            
function verificaCadastroTipo(){

    var tipo = document.getElementById("type").value;
                
    if(tipo == "" || (tipo.match(erName) == null)){
        document.getElementById("labelInsereTipo").innerHTML = " * Tipo Invalido";
        document.getElementById("labelInsereTipo").style.color = "red";
        document.getElementById("labelInsereTipo").style.visibility = "visible";
        return false;
    }
    
    document.getElementById("botaoCadastrarTipo").type = "submit";
}

function hideHelp(labelId){
    document.getElementById(labelId).style.visibility = "hidden";
}

function showHelp(labelId){
    document.getElementById(labelId).style.visibility = "visible";
}

//Verifica o campo da senha
function verificaNome(){
    var nameTemp = document.getElementById("nomeColaborador").value;
    if( (nameTemp.match(erNome))== null || nameTemp == ""){
        document.getElementById("labelNome").innerHTML = " * Nome invalido";
        document.getElementById("labelNome").style.color = "red";
        document.getElementById("labelNome").style.visibility = "visible";
    }else{
        document.getElementById("labelNome").innerHTML = " *Ok";
        document.getElementById("labelNome").style.color = "green";
        document.getElementById("labelNome").style.visibility = "visible";
    }
}

//Verifica o campo para o email
function verificaEmail(){
    
    var emailTemp = document.getElementById("emailColaborador").value;
     
    if( (emailTemp.match(erEmail))== null || emailTemp == ""){
        document.getElementById("labelEmail").innerHTML = " * Email invalido";
        document.getElementById("labelEmail").style.color = "red";
        document.getElementById("labelEmail").style.visibility = "visible";
    }else{
        document.getElementById("labelEmail").innerHTML = " *Ok";
        document.getElementById("labelEmail").style.color = "green";
        document.getElementById("labelEmail").style.visibility = "visible";
    }
    
}

//Verifica o campo senha
function verificaCampoSenha(){

    var senhaTemp = document.getElementById("senhaColaborador").value;
    
    if( (senhaTemp.match(erSenha))== null || senhaTemp == ""){
        document.getElementById("labelSenha").innerHTML = " * Senha invalida";
        document.getElementById("labelSenha").style.color = "red";
        document.getElementById("labelSenha").style.visibility = "visible";
    }else{
        document.getElementById("labelSenha").innerHTML = " *Ok";
        document.getElementById("labelSenha").style.color = "green";
        document.getElementById("labelSenha").style.visibility = "visible";
    }
}

//Verifica o campo da senha que foi repetida
function verificaSenhaRepetida(){  
    var senhaTemp = document.getElementById("senhaColaborador").value; 
    var RepitaTemp = document.getElementById("repitaSenhaColaborador").value;  
    if(senhaTemp != RepitaTemp || senhaTemp == ''){
        document.getElementById("labelRepitaSenha").innerHTML = " * Senhas nao correspondem";
        document.getElementById("labelRepitaSenha").style.color = "red";
        document.getElementById("labelRepitaSenha").style.visibility = "visible";
    }else{
        document.getElementById("labelRepitaSenha").innerHTML = " *Ok";
        document.getElementById("labelRepitaSenha").style.color = "green";
        document.getElementById("labelRepitaSenha").style.visibility = "visible";
    }
}

//Verifica cadastro colaborador     
function verificaCadastro(){
    if(verificaCadastroColaborador()){
        document.getElementById("cadastrarColaborador").type = "submit";
        window.location.href="index.jsp";
    }
}            

function verificaCadastroColaborador(){

    var email = document.getElementById("emailColaborador").value;
    var nome = document.getElementById("nomeColaborador").value;
    var sobrenome = document.getElementById("sobrenomeColaborador").value;
    var senha = document.getElementById("senhaColaborador").value;
    var repitasenha = document.getElementById("repitaSenhaColaborador").value;

    if(nome == "" || (nome.match(erNome) == null)){
        document.getElementById("labelNome").innerHTML = " * Nome invalido";
        document.getElementById("labelNome").style.color = "red";
        document.getElementById("labelNome").style.visibility = "visible";
        return false;
    }
    if(sobrenome == "" || (nome.match(erNome) == null)){
        document.getElementById("labelSobrenome").innerHTML = " * Sobrenome invalido";
        document.getElementById("labelSobrenome").style.color = "red";
        document.getElementById("labelSobrenome").style.visibility = "visible";
        return false;
    }
    if(email == "" || (email.match(erEmail) == null)){
        document.getElementById("labelEmail").innerHTML = " * E-mail invalido";
        document.getElementById("labelEmail").style.color = "red";
        document.getElementById("labelEmail").style.visibility = "visible";
        return false;
    }

    if(senha == "" || (senha.match(erSenha) == null)){
        document.getElementById("labelSenha").innerHTML = " * Senha Invalida";
        document.getElementById("labelSenha").style.color = "red";
        document.getElementById("labelSenha").style.visibility = "visible";
        
        return false;
    }
    
    if(senha != repitasenha){
        document.getElementById("labelRepitaSenha").innerHTML = " * As senhas nao sao as mesmas";
        document.getElementById("labelRepitaSenha").style.color = "red";
        document.getElementById("labelRepitaSenha").style.visibility = "visible";
        
        return false;
                 
    }

    return true;
}

//para tornar a div da tabela invisivel
function tabelaInvisivel(){
    document.getElementById("buscaResultado").style.display = "none";
}

// para tornar a div da tabela visivel
function tabelaVisivel(){
    // mostrar a div onde está a tabela que está invisivel antes da busca para não aparecer a tabela vazia
    document.getElementById("buscaResultado").style.visibility = "visible";

}

//verifica se a busca absoluta nao tem valor vazio
function verificaAbsoluta(){
    var tempAbs = document.getElementById("buscaAbsoluta").value;
    if(tempAbs == ""){
        document.getElementById("labelBuscaEntidade").innerHTML = " * Campo invalido";
        document.getElementById("labelBuscaEntidade").style.color = "red";
        document.getElementById("labelBuscaEntidade").style.visibility = "visible";
    }else{
        document.getElementById("labelBuscaEntidade").innerHTML = " *Ok";
        document.getElementById("labelBuscaEntidade").style.color = "green";
        document.getElementById("labelBuscaEntidade").style.visibility = "visible";
    }
}

//verifica a consulta por categoria
function verificaCategoria(){
    var tempCat = document.getElementById("buscaCategoria1").value;
    if(tempCat == ""){
        document.getElementById("labelBuscaCategoria").innerHTML = " * Campo invalido";
        document.getElementById("labelBuscaCategoria").style.color = "red";
        document.getElementById("labelBuscaCategoria").style.visibility = "visible";
    }else{
        document.getElementById("labelBuscaCategoria").innerHTML = " *Ok";
        document.getElementById("labelBuscaCategoria").style.color = "green";
        document.getElementById("labelBuscaCategoria").style.visibility = "visible";
    }
}

//verifica a consulta de tipos
function verificaTipos(){
    var tempTipo = document.getElementById("buscaTipos").value;
    if(tempTipo ==""){
        document.getElementById("labelBuscaTipo").innerHTML = " * Campo invalido";
        document.getElementById("labelBuscaTipo").style.color = "red";
        document.getElementById("labelBuscaTipo").style.visibility = "visible";
        
    }else{
        document.getElementById("labelBuscaTipo").innerHTML = " *Ok";
        document.getElementById("labelBuscaTipo").style.color = "green";
        document.getElementById("labelBuscaTipo").style.visibility = "visible";
    }
}

//verifica o displayname da pagina cadastroEntidade.jsp
function verificaDisplayname(){
    var tempDisplayname = document.getElementById("dpname").value;
    if(tempDisplayname ==""){
        document.getElementById("labelInsereDisplayname").innerHTML = " * Campo invalido";
        document.getElementById("labelInsereDisplayname").style.color = "red";
        document.getElementById("labelInsereDisplayname").style.visibility = "visible";
        
    }else{
        document.getElementById("labelInsereDisplayname").innerHTML = " *Ok";
        document.getElementById("labelInsereDisplayname").style.color = "green";
        document.getElementById("labelInsereDisplayname").style.visibility = "visible";
    }
}

//verifica a categoria da pagina cadastroEntidade.jsp
function verificaCategoriaCad(){
    var tempCategoria = document.getElementById("nomeCategories").value;
    if(tempCategoria ==""){
        document.getElementById("labelInsereDominio").innerHTML = " * Campo invalido";
        document.getElementById("labelInsereDominio").style.color = "red";
        document.getElementById("labelInsereDominio").style.visibility = "visible";
        
    }else{
        document.getElementById("labelInsereDominio").innerHTML = " *Ok";
        document.getElementById("labelInsereDominio").style.color = "green";
        document.getElementById("labelInsereDominio").style.visibility = "visible";
    }
}

//verifica o tipo da pagina cadastroEntidade.jsp
function verificaTiposCad(){
    var tempCategoria = document.getElementById("tipo").value;
    if(tempCategoria ==""){
        document.getElementById("labelInsereTipo2").innerHTML = " * Campo invalido";
        document.getElementById("labelInsereTipo2").style.color = "red";
        document.getElementById("labelInsereTipo2").style.visibility = "visible";
        
    }else{
        document.getElementById("labelInsereTipo2").innerHTML = " *Ok";
        document.getElementById("labelInsereTipo2").style.color = "green";
        document.getElementById("labelInsereTipo2").style.visibility = "visible";
    }
}

//verifica a categoria da pagina cadastroCategoria.jsp
function verificaCadCateg(){
    var tempCategoria = document.getElementById("categoria").value;
    if(tempCategoria ==""){
        document.getElementById("labelInsereCategoria").innerHTML = " * Campo invalido";
        document.getElementById("labelInsereCategoria").style.color = "red";
        document.getElementById("labelInsereCategoria").style.visibility = "visible";
        
    }else{
        document.getElementById("labelInsereCategoria").innerHTML = " *Ok";
        document.getElementById("labelInsereCategoria").style.color = "green";
        document.getElementById("labelInsereCategoria").style.visibility = "visible";
    }
}

//verifica o Tipo da pagina cadastroTipo.jsp
function verificaTypeCadTipo(){
    var tempCategoria = document.getElementById("cadastroTipo").value;
    if(tempCategoria ==""){
        document.getElementById("labelInsereTipo").innerHTML = " * Campo invalido";
        document.getElementById("labelInsereTipo").style.color = "red";
        document.getElementById("labelInsereTipo").style.visibility = "visible";
        
    }else{
        document.getElementById("labelInsereTipo").innerHTML = " *Ok";
        document.getElementById("labelInsereTipo").style.color = "green";
        document.getElementById("labelInsereTipo").style.visibility = "visible";
    }
}

//Verificar depois com os brothers and sisters ****************************************************************
function verificaCamposLogin(){
    var username = document.getElementById("username").value;
    var senha = document.getElementById("password").value;
    if(username.match(erEmail) == null || username == ""){
        document.getElementById("labelUsername").innerHTML = " * Campo invalido";
        document.getElementById("labelUsername").style.color = "red";
        document.getElementById("labelUsername").style.visibility = "visible";
        if(senha == "" || senha < 6){
            document.getElementById("labelPassword").innerHTML = " * Campo invalido";
            document.getElementById("labelPassword").style.color = "red";
            document.getElementById("labelPassword").style.visibility = "visible";
        }
    }
    else{
        document.getElementById("login").type = "submit";
    }
}

function verificaSobrenome(){
    var sobrenome = document.getElementById("sobrenomeColaborador").value;
    if(sobrenome == ""){
        document.getElementById("labelSobrenome").innerHTML = " * Sobrenome invalido";
        document.getElementById("labelSobrenome").style.color = "red";
        document.getElementById("labelSobrenome").style.visibility = "visible";
        
    }else{
        document.getElementById("labelSobrenome").innerHTML = " *Ok";
        document.getElementById("labelSobrenome").style.color = "green";
        document.getElementById("labelSobrenome").style.visibility = "visible";
    }
}

function verificaDisplaynameAltera(){
    
    var displayname = document.getElementById("displaynameEnt").value;
    
    if(displayname == ""){
        document.getElementById("labelAlteraDisplayname").innerHTML = " * Nome Invalido";
        document.getElementById("labelAlteraDisplayname").style.color = "red";
        document.getElementById("labelAlteraDisplayname").style.visibility = "visible";
        return false;
    }else{
        document.getElementById("labelAlteraDisplayname").style.visibility = "hidden";
        return true;
    }
}

function verificaDominioAlterar(){
    var dominioAlterar = document.getElementById("dominioEnt").value;
    
    if(dominioAlterar == ""){
        document.getElementById("labelAlteraDominio").innerHTML = " * Categoria Invalida";
        document.getElementById("labelAlteraDominio").style.color = "red";
        document.getElementById("labelAlteraDominio").style.visibility = "visible";
        return false;
    }else{
        document.getElementById("labelAlteraDominio").style.visibility = "hidden";
        return true;
    }    
}

function verificaTipoAlterar(){
    
    var tipoAlterar = document.getElementById("tipoEnt").value;    

    if(tipoAlterar == ""){
        document.getElementById("labelAlteraTipo2").innerHTML = " * Tipo Invalido";
        document.getElementById("labelAlteraTipo2").style.color = "red";
        document.getElementById("labelAlteraTipo2").style.visibility = "visible";
        return false;
    }else{
        document.getElementById("labelAlteraTipo2").style.visibility = "hidden";
        return true;
    }    
}

function verificaAlteracaoEntidade(tipoAltera){    
    if(verificaDisplaynameAltera() && verificaDominioAlterar() && verificaTipoAlterar()){
        document.getElementById("alteraEntidade").value = tipoAltera;
        document.getElementById("alterarEnt").type = "submit";
    }   
}

//verifica se a busca absoluta nao tem valor vazio
function verificaRelativa(){
    var tempRel = document.getElementById("buscarelativa").value;
    if(tempRel == ""){
        document.getElementById("labelBuscaRelativa").innerHTML = " * Campo invalido";
        document.getElementById("labelBuscaRelativa").style.color = "red";
        document.getElementById("labelBuscaRelativa").style.visibility = "visible";
    }else{
        document.getElementById("labelBuscaRelativa").innerHTML = " *Ok";
        document.getElementById("labelBuscaRelativa").style.color = "green";
        document.getElementById("labelBuscaRelativa").style.visibility = "visible";
    }
}
